package com.pignest.hotel.ui.reception;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.LivingCustom;
import com.pignest.hotel.beans.LivingSpecifics;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbLiveGoodsBorrow;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.definition.RoomUseStatus;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class LivingManagerVbox extends Vbox implements EventListener {

	/**
	 * 入住管理
	 */
	private static final long serialVersionUID = 6712179774131184416L;
	
	private ReceptionService receptionService;
	private Listbox LivingListbox;
	private Paging pagLiving;
	/**查询项****/
	private Textbox qRoomName;//房间名
	private Listbox qRoomTypeList;//房间类型
	private Listbox qLiveStateList;//入住状态
	private Listbox qOpenStateList;
	private Textbox qCustormName;//客户姓名
	private Listbox qGender;//性别
	private Datebox qStartLiveDate;//入住开始日期
	private Datebox qEndLiveDate;//入住截止日期
	/**按钮项*/
	private Button madeCardBtn;//制作门卡
	private Button printAcountBtn;//打印凭证
	private Button addSameRoomCustomBtn;
	private Button delteSameRoomCustomBtn;
	private Button changeOpenTypeBtn;
	private Button cancelBtn;
	private Button modLivingBtn;
	/**TAB项*/
	private Listbox AccoutsListbox;//消费情况包括房费
	private Listbox LivingHistoryListbox;
	private Listbox GoodsBorrowListbox;
	private Listbox CustonListbox;
	
	
    public LivingManagerVbox()
    {
    	receptionService =Services.getReceptionService();
    }
    public void onCreate() {
    	LivingListbox=(Listbox)getFellow("LivingListbox");
		pagLiving    =(Paging)getFellow("pagLiving");
		qRoomName    =(Textbox)getFellow("qRoomName");
		qRoomTypeList=(Listbox)getFellow("qRoomTypeList");
		qLiveStateList=(Listbox)getFellow("qLiveStateList");
		qOpenStateList=(Listbox)getFellow("qOpenStateList");
		qCustormName  =(Textbox)getFellow("qCustormName");
		qGender       =(Listbox)getFellow("qGender");
		qStartLiveDate=(Datebox)getFellow("qStartLiveDate");
		qEndLiveDate  =(Datebox)getFellow("qEndLiveDate");
		cancelBtn=(Button)getFellow("cancelBtn");
		madeCardBtn =(Button)getFellow("madeCardBtn");
		printAcountBtn=(Button)getFellow("printAcountBtn");
		addSameRoomCustomBtn =(Button)getFellow("addSameRoomCustomBtn");
		delteSameRoomCustomBtn=(Button)getFellow("delteSameRoomCustomBtn");
		changeOpenTypeBtn=(Button)getFellow("changeOpenTypeBtn");
		modLivingBtn=(Button)getFellow("modLivingBtn");
		
		AccoutsListbox =(Listbox)getFellow("AccoutsListbox");
		LivingHistoryListbox=(Listbox)getFellow("LivingHistoryListbox");
		GoodsBorrowListbox =(Listbox)getFellow("GoodsBorrowListbox");
		CustonListbox =(Listbox)getFellow("CustonListbox");
		final int PAGE_SIZE = pagLiving.getPageSize();
		pagLiving.addEventListener("onPaging", new EventListener() {
	      public void onEvent(Event event) {
	        	PagingEvent pe = (PagingEvent) event;
	        	int pgno = pe.getActivePage();
	        	int start = pgno*PAGE_SIZE;
	        	query(start,PAGE_SIZE);
	      	}
	    	});
		query(0,PAGE_SIZE);
    }
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			
		}
	}
	public void query(int start,int pageSize)
	{
		LivingListbox.getItems().clear();
		String sRoomName =qRoomName.getValue();
		String sLivingState =(String)UIUtils.getSelectedValue(qLiveStateList);
		String sRoomTypeId =(String)UIUtils.getSelectedValue(qRoomTypeList);
		String sCustormName =(String)qCustormName.getValue();
        String sSex =(String)UIUtils.getSelectedValue(qGender);
        String oType =(String)UIUtils.getSelectedValue(qOpenStateList);
        Date startLivingDate=qStartLiveDate.getValue();
        Date endLivingDate=qEndLiveDate.getValue();
        int Total =receptionService.queryLiving(sCustormName, sLivingState, sRoomName, sRoomTypeId, sSex,startLivingDate, endLivingDate,oType).size();
        pagLiving.setTotalSize(Total);
        List <Living> ls =receptionService.queryPageHQL(sCustormName, sLivingState, sRoomName, sRoomTypeId, sSex, startLivingDate, endLivingDate,oType, start, pageSize);
        int index=1;
		for(final Living l:ls){
			  
			  Listitem listitem = new Listitem();
			  listitem.setValue(l);
			  listitem.setParent(LivingListbox);
			  listitem.appendChild(new Listcell(String.valueOf(index)));
			  listitem.appendChild(new Listcell(l.getRoom().getName()));
			  listitem.appendChild(new Listcell(l.getRoom().getType().getName()));
			  listitem.appendChild(new Listcell(LivingState.valueOf(l.getLiveState()).getDisplayName()));
			  listitem.appendChild(new Listcell(l.getCustormName()));
			  listitem.appendChild(new Listcell(OpenRoomType.valueOf(l.getOpenRoomType()).getDisplayName()));
			  listitem.appendChild(new Listcell(Utils.toPrice(l.getRoomPrice())));
			  listitem.appendChild(new Listcell(DateUtil.toString(l.getLiveTime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			  listitem.appendChild(new Listcell(DateUtil.toString(l.getLeaveDate(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			  listitem.appendChild(new Listcell(DateUtil.toString(l.getExpectLeaveDate(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			  listitem.appendChild(new Listcell(DateUtil.toString(l.getGrantDate(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			   
			  listitem.appendChild(new Listcell(RoomUseStatus.valueof(l.getRoom().getUseStatus()).getName()));
			  listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
				   public void onEvent(Event event) throws Exception {
					   chekcButon(l);
					   queryAccount(l);
					   queryHistoryLiving(l);
					   queryGoods(l);
					   queryCurCustom(l);
				   }
			   });
			  index++;
		}
	}
	public void chekcButon(Living l)
	{
		if(l.getLiveState().equals(LivingState.LIVING.name()))
		{
			madeCardBtn.setDisabled(false);
			printAcountBtn.setDisabled(true);
			addSameRoomCustomBtn.setDisabled(false);
			delteSameRoomCustomBtn.setDisabled(false);
			changeOpenTypeBtn.setDisabled(false);
			cancelBtn.setDisabled(false);
			modLivingBtn.setDisabled(false);
		}else
		{
			madeCardBtn.setDisabled(true);
			printAcountBtn.setDisabled(false);
			addSameRoomCustomBtn.setDisabled(true);
			delteSameRoomCustomBtn.setDisabled(true);
			changeOpenTypeBtn.setDisabled(true);
			cancelBtn.setDisabled(true);
			if(l.getLiveState().equals(LivingState.PARTCHECKOUT.name())){
				modLivingBtn.setDisabled(false);
			}else
				modLivingBtn.setDisabled(true);
		}
	}
	/**
	 * 核对消费详细信息
	 * **/
	public void queryAccount(Living living){
		if(living!=null){
			AccoutsListbox.getItems().clear();
			List<TbAccounts> Ass =receptionService.queryAccountsNotDeposit(living);
			if(Ass==null)
				return;
			for(TbAccounts as:Ass){
				Listitem listitem = new Listitem();
				   listitem.setValue(as.getId());
				   listitem.setParent(AccoutsListbox);
				   listitem.appendChild(new Listcell(as.getName()));
				   listitem.appendChild(new Listcell(Utils.toPrice(as.getPrice())));
				   listitem.appendChild(new Listcell(Utils.toPrice(as.getPrice2())));
				   /*Integer i =as.getCounts();
			    	if(i!=null)*/
			    		listitem.appendChild(new Listcell(String.valueOf(as.getCounts())));
			    	/*else
			    		listitem.appendChild(new Listcell(""));*/
			    	
				   listitem.appendChild(new Listcell(as.getEndstate()));
				   listitem.appendChild(new Listcell(as.getRemark()));
			}
		}
	}
	/**
	 * 查询物品借用情况 
	 * **/
	public void queryGoods(Living living)
	{
		if(living!=null){
			GoodsBorrowListbox.getItems().clear();
			List <TbLiveGoodsBorrow> list =receptionService.queryGoodsBorrow(living);
			for(TbLiveGoodsBorrow gb:list)
			{
				Listitem listitem = new Listitem();
		    	listitem.setValue(gb.getId());
		    	listitem.setParent(GoodsBorrowListbox);
		    	listitem.appendChild(new Listcell(gb.getTbGoods().getName()));
		    	listitem.appendChild(new Listcell(DateUtil.toString(gb.getTime())));
		    	listitem.appendChild(new Listcell(DateUtil.toString(gb.getBacktime())));
		    	Integer i=gb.getCount();
		    	if(i!=null)
		    	   listitem.appendChild(new Listcell(String.valueOf(gb.getCount())));
		    	else
		    		listitem.appendChild(new Listcell(""));
			}
		}
	}
	 public void queryHistoryLiving(Living living){
		 if(living!=null){
				this.LivingHistoryListbox.getItems().clear();
				StringBuffer hql=new StringBuffer();
				hql.append(" from LivingSpecifics b where b.living.id='"+living.getId()+"'");
				hql.append(" order by b.operateDate desc");
				List <LivingSpecifics> list=receptionService.find(hql.toString());
				for( LivingSpecifics s:list){
				Listitem listitem = new Listitem();
				listitem.setParent(LivingHistoryListbox);
				listitem.appendChild(new Listcell(s.getRoom()));//房间名
				listitem.appendChild(new Listcell(s.getOperateType()));//操作类型
				listitem.appendChild(new Listcell(Utils.toPrice(s.getRoomPrice())));//房价
				listitem.appendChild(new Listcell(Utils.toPrice(s.getPay())));//金额
				//listitem.appendChild(new Listcell(DateUtil.toString(s.getGrantDate())));//授权时间"
				listitem.appendChild(new Listcell(DateUtil.toString(s.getLeaveDate())));//离开时间
				listitem.appendChild(new Listcell(s.getRemark()));//备注
				listitem.appendChild(new Listcell(DateUtil.toString(s.getOperateDate())));//备注
				}
				
			}
	 }
	 /**
		 * 根据入住查询入住客人
		 * */
		public void queryCurCustom(Living l){
			CustonListbox.getItems().clear();
			if(l!=null){
				StringBuffer hql = new StringBuffer();
				hql.append(" from LivingCustom b where 1=1 ").append(" and b.living.id='").append(l.getId()).append("'");
				List<LivingCustom> lclist = receptionService.find(hql.toString());
				for(LivingCustom lc:lclist){
					 Listitem listitem = new Listitem();
					 listitem.setValue(lc.getId());
					 listitem.setParent(CustonListbox);
					 listitem.appendChild(new Listcell(""));
					 listitem.appendChild(new Listcell(lc.getName()));
					 listitem.appendChild(new Listcell(lc.getSex()));
					 listitem.appendChild(new Listcell(lc.getIshost()));
					 listitem.appendChild(new Listcell(lc.getCertificatetype()));
					 listitem.appendChild(new Listcell(lc.getCertificateno()));
					 listitem.appendChild(new Listcell(lc.getPhone()));
					 if(lc.getIshost().equals("是")){
						 listitem.setCheckable(false);
						 listitem.setStyle("background-color:#dddddd;");
					 }
				}
			}
		}
}
